#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <unordered_map>
#include <string>
#include "hpf_global.hpp"  //一些全局/通用定义
#include "hpf_string.hpp"
/*
	CConfig用于读取配置文件
*/

class CConfig
{
//---------------------------------------------------

public:
	CConfig()
    {

    }
	~CConfig()
    {
        for(auto pos = m_hashConfig.begin(); pos != m_hashConfig.end(); ++pos)
        {		
            delete pos->second;
        }
    }
public:	

//---------------------------------------------------
public:
    bool Load(const char *pconfName) //装载配置文件
    {
        FILE *fp;
        fp = fopen(pconfName,"r");
        if(fp == NULL)
            return false;
    
        //每一行配置文件读出来都放这里
        char  linebuf[501];   //每行配置都不要太长，保持<500字符内，防止出现问题
        
        //走到这里，文件打开成功 
        while(!feof(fp))  //#直到读取到文件末尾
        {    
            /*
            and NULL on error or when end of file occurs while no characters have been read
            */
            if(fgets(linebuf,500,fp) == NULL) //每次读一行，一行最多不要超过500个字符 
                continue;
    
            if(linebuf[0] == 0) //空行？
                continue;
    
            //处理注释行 # *linebuf表示首字符
            if(*linebuf==';' || *linebuf==' ' || *linebuf=='#' || *linebuf=='\t'|| *linebuf=='\n')
                continue;
            
        lblprocstring:
            //屁股后边若有换行，回车，空格等都截取掉
            if(strlen(linebuf) > 0)
            {
                //#定位数组末尾
                if(linebuf[strlen(linebuf)-1] == 10 /*\n*/|| linebuf[strlen(linebuf)-1] == 13 /*\r*/|| linebuf[strlen(linebuf)-1] == 32/* */) 
                {
                    linebuf[strlen(linebuf)-1] = 0;
                    goto lblprocstring;
                }		
            }
            if(linebuf[0] == 0)
                continue;
            if(*linebuf=='[') //[开头的也不处理
                continue;
    
            //这种 “ListenPort = 5678”走下来；
            char *ptmp = strchr(linebuf,'=');//# function returns a pointer to the first occurrence of the character c in the string s.
            if( nullptr != ptmp)
            {
                //LPCConfItem相当于“CConfItem *”，所以千万不要写成LPCConfItem *p_confitem  = new CConfItem;
                LPCConfItem p_confitem = new CConfItem;                    //注意前边类型带LP，后边new这里的类型不带
                memset(p_confitem,0,sizeof(CConfItem));
                strncpy(p_confitem->ItemName,linebuf,(int)(ptmp-linebuf)); //等号左侧的拷贝到p_confitem->ItemName
                strcpy(p_confitem->ItemContent,ptmp+1);                    //等号右侧的拷贝到p_confitem->ItemContent
    
                Rtrim(p_confitem->ItemName); //自定义函数 截取字符串尾部空格
                Ltrim(p_confitem->ItemName); //自定义函数 截取字符串首部空格
                Rtrim(p_confitem->ItemContent);
                Ltrim(p_confitem->ItemContent);
                
                m_hashConfig[p_confitem->ItemName] = p_confitem;
            } //end if
        } //end while(!feof(fp)) 
    
        fclose(fp); //这步不可忘记
        return true;
    }
	std::string GetString(const std::string& strItemname)
    {
        auto pos = m_hashConfig.find(strItemname);

        return m_hashConfig.end() == pos ? nullptr : pos->second->ItemContent;
    }

	int GetIntDefault(const std::string& strItemname,const int def)
    {
        auto pos = m_hashConfig.find(strItemname);

        return m_hashConfig.end() == pos ? def : atoi(pos->second->ItemContent);       
    }

public:
	std::unordered_map<std::string,LPCConfItem> m_hashConfig; //存储配置信息的列表

};





